package org.action.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.action.BaseAction;
import org.biz.news.INewsBiz;
import org.hibernate.entity.Column;
import org.hibernate.entity.NewsTitle;
import org.hibernate.entity.Suggestion;
import org.util.file.FileUtil;

import common.util.page.WebPagination;

/**
 * 
 * 文章详情
 * 
 * @Title: NewsAction.java
 * @Description:新闻系统(一期)
 * @Copyright: Copyright (c) 2011
 * @Company:leyikao
 * @author admin
 * @date Oct 31, 2012 1:45:31 PM
 * @version V4.0
 */
public class NewsAction extends BaseAction {

	private static final long serialVersionUID = -3159569490825846866L;

	private INewsBiz newsBiz = null;
	private WebPagination page = new WebPagination(); // 分页对象

	private NewsTitle news = new NewsTitle();
	private Suggestion suggestion = new Suggestion();

	private Map<Column,List<Column>> parentColumn = new HashMap<Column,List<Column>>();
	/** 子栏目 */
	private List<Column> lstChannelColumns = new ArrayList<Column>();
	// 当前新闻的栏目
	Column currentColumn = null;
	/** 上篇文章  */
	private NewsTitle foreNews = null;
	/** 下篇文章 */
	private NewsTitle nextNews = null;
	/** 上篇文章  */
	private Suggestion foreNews1 = null;
	/** 下篇文章 */
	private Suggestion nextNews1 = null;
	/** 文章列表  */
	//private Integer intArticlesLimit = 5;
	
	
	public void setParentColumn(Map<Column,List<Column>> parentColumn){
		this.parentColumn = parentColumn;
	}
    public Map<Column,List<Column>> getParentColumn(){
    	return this.parentColumn;
    }

	public WebPagination getPage() {
		return page;
	}

	public void setPage(WebPagination page) {
		this.page = page;
	}

	public NewsTitle getNews() {
		return news;
	}

	public void setNews(NewsTitle news) {
		this.news = news;
	}

	public Suggestion getSuggestion() {
		return suggestion;
	}
	public void setSuggestion(Suggestion suggestion) {
		this.suggestion = suggestion;
	}
	public INewsBiz getNewsBiz() {
		return newsBiz;
	}

	public void setNewsBiz(INewsBiz newsBiz) {
		this.newsBiz = newsBiz;
	}
	public List<Column> getLstChannelColumns() {
		return lstChannelColumns;
	}
	public void setLstChannelColumns(List<Column> lstChannelColumns) {
		this.lstChannelColumns = lstChannelColumns;
	}
	/**
	 * @return the foreNews
	 */
	public NewsTitle getForeNews() {
		return foreNews;
	}
	/**
	 * @param foreNews the foreNews to set
	 */
	public void setForeNews(NewsTitle foreNews) {
		this.foreNews = foreNews;
	}

	public Suggestion getForeNews1() {
		return foreNews1;
	}
	public void setForeNews1(Suggestion foreNews1) {
		this.foreNews1 = foreNews1;
	}
	public Suggestion getNextNews1() {
		return nextNews1;
	}
	public void setNextNews1(Suggestion nextNews1) {
		this.nextNews1 = nextNews1;
	}
	/**
	 * @return the nextNews
	 */
	public NewsTitle getNextNews() {
		return nextNews;
	}

	/**
	 * @param nextNews the nextNews to set
	 */
	public void setNextNews(NewsTitle nextNews) {
		this.nextNews = nextNews;
	}

	public Column getCurrentColumn() {
		return currentColumn;
	}

	public void setCurrentColumn(Column currentColumn) {
		this.currentColumn = currentColumn;
	}

	/**
	 * 新闻详情
	 * 
	 * @return
	 */
	public String toDetail() {
		newsDetail();
		return "toDetail";
	}
	/**
	 * 简介
	 * @return
	 */
	public String toAnswer() {
		queAnsDetail();
		return "toAnswer";
	}
	
	/**
	 * 创业基地
	 * @return
	 */
	public String toBase() {
		baseDetail();
		return "toBase";
	}
	
	/**
	 * 创客空间
	 * @return
	 */
	public String toMaker(){
		makerDetail();
		return "toMaker";
	}
	
	public void makerDetail(){
		//this.news = (NewsTitle) this.newsBiz.selObjectById(NewsTitle.class,this.news.getId());
		//this.set("newContents", this.newsBiz.selNewsContentsByTid(this.news.getId(), this.page.getPageNo(), this.page.getPageSize()));
		//this.set("newArticleService", this.newsBiz.selNewsByCidNoPic(18, 9));
		//this.set("newArticleMaker", this.newsBiz.selNewsByCidNoPic(19, 9));
		this.set("newArticleMaker", this.newsBiz.selNewsByCidNoPic(10, 9));
		this.initMenu();
		
	}
	
	public void baseDetail(){
		this.news = (NewsTitle) this.newsBiz.selObjectById(NewsTitle.class,this.news.getId());
		this.set("newContents", this.newsBiz.selNewsContentsByTid(this.news.getId(), this.page.getPageNo(), this.page.getPageSize()));
		this.newArticle(news.getCid());
		this.initMenu();
	}
	
	public void queAnsDetail(){
		this.suggestion = (Suggestion) this.newsBiz.selObjectById(Suggestion.class,this.news.getId());

		foreNews1 = newsBiz.loadForeAnswer(this.suggestion.getId());// 上一篇
		nextNews1 = newsBiz.loadNextAnswer(this.suggestion.getId());// 下一篇
		
		this.initMenu();
	}
	/**
	 * 下载文件Action
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	public void toDownload() throws Exception{
		this.news = (NewsTitle) this.newsBiz.selObjectById(NewsTitle.class,this.news.getId());
		String path = this.news.getPicurl();
		System.out.println(path);
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		request.setAttribute("path", path);
		FileUtil.downLoadFile(request, response);
	}
	
	public void newsDetail(){
		this.news = (NewsTitle) this.newsBiz.selObjectById(NewsTitle.class,this.news.getId());

		this.page.setCount(this.news.getPages());
		this.page.setPageSize(1);
		this.set("newContents", this.newsBiz.selNewsContentsByTid(this.news.getId(), this.page.getPageNo(), this.page.getPageSize()));
		this.page.setUrl(request.getContextPath() + "/article/"+ this.news.getId());
		this.set("page_style", this.page.newsPros());
		
		//this.initNewsetArticle();
		//this.initlstRecommendNews();
		// 获得栏目
		currentColumn = this.newsBiz.searchColumnByCid(news.getCid());
		if(currentColumn.getMenuColumns() != null){
		   lstChannelColumns = this.newsBiz.listChildColumns(currentColumn.getMenuColumns());
		   parentColumn.put(currentColumn.getMenuColumns(), lstChannelColumns);
		   this.set("navLists", parentColumn);
		}else{
		   lstChannelColumns = this.newsBiz.listChildColumns(currentColumn);
		   parentColumn.put(currentColumn, lstChannelColumns);
		   this.set("navLists", parentColumn);
		}
		// 当前位置
		if(currentColumn.getMenuColumns() == null){
			this.set("navFist",currentColumn);
		}else{
			this.set("navFist", currentColumn.getMenuColumns());
			this.set("navSec", currentColumn);
		}
		
		// 导航栏选择
		Integer intNavChoiceID = null;
		if(currentColumn.getMenuColumns()==null){
			intNavChoiceID = currentColumn.getId();
		} else {
			intNavChoiceID = currentColumn.getMenuColumns().getId();
		}
		this.set("intNavChoiceID", intNavChoiceID);
		foreNews = newsBiz.loadForeNews(news.getCid(),this.news.getId());// 上一篇
		nextNews = newsBiz.loadNextNews(news.getCid(),this.news.getId());// 下一篇
		// 本文标签;
		//this.myTag(this.news);
		// 更新文章访问量;
		NewsTitle nt = this.news;
		nt.setPv(nt.getPv() + 1);
		this.newsBiz.doUpdateObject(nt);
		
		this.initMenu();

	}

	/**
	 * 新闻详情,分页，不更新访问量
	 * 
	 * @return
	 */
	public String toDetailByPage() {
		this.news = (NewsTitle) this.newsBiz.selObjectById(NewsTitle.class,
				this.news.getId());

		this.page.setCount(this.news.getPages());
		this.page.setPageSize(1);
		this.set("newContents", this.newsBiz.selNewsContentsByTid(this.news
				.getId(), this.page.getPageNo(), this.page.getPageSize()));
		this.page.setUrl(request.getContextPath() + "/article/"
				+ this.news.getId());
		this.set("page_style", this.page.newsPros());

		// 相关文章;
		this.relateArticle(this.news.getKeywords());

		// 相关标签;
		//this.relateTag(this.news.getCid());

		// 最新文章;
		this.newArticle(this.news.getCid());

		return "toDetail";
	}

	/**
	 * 相关标签文章;
	 */
	public void relateArticle(String keywords) {
		this.set("releateArticles", this.newsBiz.selRelateArticleByKid(
				keywords, 10));
	}

	/**
	 * 相关标签
	 */
/*	public void relateTag(int cid) {
		this.set("releateTags", this.newsBiz.selKeywordsByCid(cid));
	}*/

	/**
	 * 本栏目最新文章
	 */
	public void newArticle(int cid) {
		this.set("newArticleNoPic", this.newsBiz.selNewsByCidNoPic(cid, 9));
		//this.set("newArticlePic", this.newsBiz.selNewsByCidPic(cid));
	}
	
	
	/**
	 * 查看所有的标签
	 * 
	 * @return
	 */
/*	public String doSelTags() {
		this.set("tags", this.newsBiz.selAllTags());
		return "doSelTags";
	}*/

	/**
	 * 添加评论;
	 */
	/*public void doAddComment() {

		Comment newComment = new Comment();
		newComment.setTid(this.comment.getTid());
		User user = (User) request.getSession().getAttribute("onlineUser");
		if (user != null) {
			newComment.setUid(user.getId());
			newComment.setUname(user.getAccountNumber());
		} else {
			newComment.setUname("游客");
		}
		newComment.setContents(StringUtils.htmlspecialchars(this.comment
				.getContents()));
		newComment.setPubtime(new Date(System.currentTimeMillis()));
		newComment.setIfPass(1);
		boolean result = this.newsBiz.doAddObject(newComment); // 添加执行操作;
		try {
			response.sendRedirect(request.getContextPath() + "/article/"
					+ this.news.getId() + ".html");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	/**
	 * 展示最新文章
	 */
	/*private void initNewsetArticle(){
		
		// 最新文章，限定5篇
		List<NewsTitle> lstArticles =  this.newsBiz.listNewestArciles(intArticlesLimit);
		this.set("lstArticles",lstArticles );
	}*/
	/**
	 * 推荐
	 */
	/*private void initlstRecommendNews(){
		// 推荐，限定5篇
		List<NewsTitle> lstRecommendNews =  this.newsBiz.searchByRecommend(intArticlesLimit);
		this.set("lstRecommendNews",lstRecommendNews );
	}*/
	private void initMenu(){
		lstChannelColumns=null;
		lstChannelColumns=this.newsBiz.listChildColumns();
		List<Column> towColumns=new ArrayList();
		List<Column> threeColumns=new ArrayList();
		for(Column fc:lstChannelColumns){
			fc.setParentId(fc.getMenuColumns()==null?null:fc.getMenuColumns().getId());

			if(fc.getParentId()!=null){
				for(Column fcc:lstChannelColumns){
					if(fcc.getId().equals(fc.getParentId())){
						if(fcc.getParentId()==null){
							towColumns.add(fc); 
							break;
						}else{
							threeColumns.add(fc);break;
						}
					}
				}
			}
				
		}
		for(Column fc:lstChannelColumns){
			for(Column towfc:towColumns){
				if(fc.getId().equals(towfc.getParentId())){
					fc.ischi=true;
				}
			}
		}
		this.set("towColumns", towColumns);
		this.set("threeColumns", threeColumns);
	}
}
